package jnpf.database.source.impl;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.handler.TableNameHandler;
import jnpf.database.constant.DbAliasConst;
import jnpf.database.data.DataSourceContextHolder;
import jnpf.database.enums.DbAliasEnum;
import jnpf.database.enums.datatype.DataTypeEnum;
import jnpf.database.enums.datatype.DtMysqlEnum;
import jnpf.database.model.DataTypeModel;
import jnpf.database.model.DbTableFieldModel;
import jnpf.database.model.DbTableModel;
import jnpf.database.source.DbBase;
import jnpf.database.sql.SqlBase;
import jnpf.database.sql.impl.SqlMySQL;
import jnpf.database.util.CommonUtil;
import jnpf.database.util.DataSourceUtil;
import jnpf.util.StringUtil;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;

/**
 * MySQL模型
 *
 * @author JNPF开发平台组 YY
 * @version V3.2.0
 * @copyright 引迈信息技术有限公司
 * @date 2021/10/06
 */
@Slf4j
public class DbMySQL extends DbBase {

    public static final String DB_ENCODE = "MySQL";

    public static final String CONN_URL = "jdbc:mysql://{host}:{port}/{dbname}?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&serverTimezone=GMT%2B8&useSSL=false";
    public static final String CONN_URL2 = "jdbc:mysql://{host}:{port}/{dbname}?useUnicode=true&autoReconnect=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8";


    @Override
    public void setDbType() {
        mpDbType = DbType.MYSQL;
        connUrlEncode = "mysql";
        dbEncode = DB_ENCODE;
        driver = "com.mysql.cj.jdbc.Driver";

    }

    @Override
    public SqlBase getSqlBase() {
        return new SqlMySQL(this);
    }

    @Override
    public DataTypeModel getDataTypeModel(DataTypeEnum dte){
        return dte.getDtMysqlEnum().getDataTypeModel();
    }

    @Override
    public DataTypeModel getDataTypeModel(String dbFieldType) {
        for(DtMysqlEnum dataType : DtMysqlEnum.values()){
            if(dbFieldType.equals(dataType.getDbFieldType())){
                return dataType.getDataTypeModel();
            }
        }
        return null;
    }

    @Override
    public String getConnUrl(String prepareUrl, String host, Integer port, String dbName) {
        if (StringUtil.isEmpty(prepareUrl)) {
            prepareUrl = CONN_URL;
        }
        return super.getConnUrl(prepareUrl, host, port, dbName);
    }

    @Override
    public void setDynamicMap(HashMap<String, TableNameHandler> map, DbTableModel dbTableModel) {
        map.put(dbTableModel.getTable(), (sql, tableName) -> DataSourceContextHolder.getDatasourceName()+"."+dbTableModel.getTable());
    }

    @Override
    public DbTableFieldModel getFieldModel(ResultSet result) throws SQLException{
        DbTableFieldModel model = new DbTableFieldModel();
        // 精度
        String precision = result.getString(DbAliasConst.PRECISION);
        DataTypeModel dataTypeModel = getDataTypeModel(result.getString(DbAliasEnum.DATA_TYPE.asByDb(this)));
        if(dataTypeModel != null){
            if(!StringUtil.isEmpty(precision) || dataTypeModel.getPrecisionMax() != null) {
                model.setDataLength(precision + "," + result.getString(DbAliasConst.DECIMALS));
            }
        }
        return model;
    }

    @Override
    public String getDbTime(Connection conn) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s') as TIME");
        return dbTimeCommon(conn,sql);
    }

    @Override
    public String getReplaceSql(String sql, String table, DataSourceUtil dbSourceOrDbLink) {
        /**
         * 说明：一个实例对应多个数据库
         * 表空间默认不常用,模式schema也没有
         */
        return CommonUtil.getReplaceSql(sql,table,dbSourceOrDbLink,
                true,
                false,
                false);
    }


}
